package com.jiuyuan.config.security;

import com.jiuyuan.entity.Admin;
import com.jiuyuan.service.IAdminService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: hrms
 * @description: JWT过滤器
 * @author: Feng
 * @create: 2023-02-15 11:10
 **/

//@Configuration
public class JWTAuthenticationFilter extends OncePerRequestFilter {
//    @Autowired
//    IAdminService adminService;
    @Autowired
    UserDetailsService userDetailsService;


    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String head;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        //存在token
        String token = request.getHeader(tokenHeader);
        if(null!=token && token.startsWith(head)){
            token = token.substring(head.length());
            Claims claims = JWTTokenUtils.getClaimsByToken(token);

//            String username = claims.getSubject(); 多封装一层
            String username = JWTTokenUtils.getUsernameFromToken(token);
            //token存在用户名，但未登陆
            if(null!=username && null == SecurityContextHolder.getContext().getAuthentication()){
                //登陆
//                Admin admin = adminService.getAdminByUsername(username); 用userDetailService
                UserDetails admin = userDetailsService.loadUserByUsername(username);
                //验证token是否有效
                if(JWTTokenUtils.isValid(token,admin)){
                    //更新
                    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(admin,null,admin.getAuthorities());
//                    authenticationToken.setDetails(new WebAuthenticationDetails(request));
                    authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                    SecurityContextHolder.getContext().setAuthentication(authenticationToken);
                }
            }
        }
        doFilter(request,response,filterChain);
    }
}



























